﻿using System;
using System.Drawing;

namespace ru.yourpictures.Core.Imaging.Filters.PhotoFilters
{
    [FilterImplementation(GdiPlusImplemented = false, LowLevelImplemented = true)]
    public class RedEyeReduction : FilterBase
    {
        private Rectangle[] _redEyes = null;
        private SimpleColor _colorToRemove = SimpleColor.Red;

        #region Public properties
        /// <summary>
        /// Which color will be filtered out
        /// </summary>
        public SimpleColor ColorToRemove
        {
            get { return _colorToRemove; }
            set { _colorToRemove = value; }
        }

        /// <summary>
        /// Array of regions. For each region red color will be eliminated
        /// </summary>
        public Rectangle[] RedEyes
        {
            get { return _redEyes; }
            set { _redEyes = value; }
        }
        #endregion

        #region Ctor
        public RedEyeReduction(Rectangle[] regions, SimpleColor colorToRemove)
        {
            _redEyes = regions;
            _colorToRemove = colorToRemove;
        }
        public RedEyeReduction(Rectangle[] regions)
        {
            _redEyes = regions;
        }
        public RedEyeReduction() { }
        #endregion

        #region FilterImplementation
        protected override void FilterImplementation()
        {
            ValidatePixelFormat(_b.PixelFormat);

            // For each of these regions we'll set selected simple color to 0;
            int index = 0;
            int offset = (int)_colorToRemove;
            foreach (Rectangle rect in _redEyes)
            {
                for (int y = rect.Top; y <= rect.Bottom; y++)
                {
                    for (int x = rect.Left; x <= rect.Right; x++)
                    {
                        index = y * _stride + x * _bytesPerPixel;
                        _pixels[index + offset] = 0;
                    }
                }
            }
        }
        #endregion

        #region Public Filter Methods
        public override void Apply(Bitmap b)
        {
            b = Convert(b);
            _b = b;
        }

        public override Bitmap Convert(Bitmap b)
        {
            throw new NotImplementedException("GDI+ mode is not implemented");
        }
        #endregion
    }
}
